var express = require('express');
var router = express.Router();
var path=require('path');
var CONFIG = require('../../common/config');
var CODE = require('../../common/code');
var checkUtil = require('./util/checkUtil');
var luaUtil = require('./util/luaUtil');
var luaSearchUtil = require('./util/luaSearchUtil');

//------------------------------------------------------------------
// for test
router.get('/test', function(req, res, next) {

    var func = req.query.func;

    luaUtil.test(func, function(err, ret){
        if(err) {
            return luaUtil.ret_func_json(0,CODE.OPERATION_FAILED,{},res);
        }
        return luaUtil.ret_func_json(1,CODE.OPERATION_SUCCESS,ret,res);
    });
});
//------------------------------------------------------------------
// 搜索相关接口：test2
router.get('/test2', function(req, res, next) {
    var func = req.query.func;
    var para = {};
    if('lua2_search_design' == func){
        // http://localhost:3000/lua/test2?func=lua2_search_design&types=1,3&labels=1111,1112&price_type=-1&keywords=1113,1114&page_num=0&page_size=10
        para = {
            types:req.query.types,
            labels:req.query.labels,
            price_type:req.query.price_type,
            keywords:req.query.keywords,
            page_num:req.query.page_num || 0,
            page_size:req.query.page_size  || 10,
        };
    }

    luaSearchUtil.test(func, para,  function(err, ret){
        if(err) {
            return luaUtil.ret_func_json(0,CODE.OPERATION_FAILED,{},res);
        }
        return luaUtil.ret_func_json(1,CODE.OPERATION_SUCCESS,ret,res);
    });
});



//------------------------------------------------------------------
/**
 * 添加活动场次信息(包括活动商品信息)
 * 参数 actId: 活动场次id
 */
function addActivity(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");   //设置跨域访问

    var actId = checkUtil.numberFromReq(req, 'actId')||0;
    if(actId<=0) {
        return luaUtil.ret_func_json(0,'参数错误',{},res);
    }

    luaUtil.addActivityAndItems(actId, function(err, ret){
        if(err) {
            return luaUtil.ret_func_json(0,CODE.OPERATION_FAILED,{},res);
        }
        return luaUtil.ret_func_json(1,CODE.OPERATION_SUCCESS,ret,res);
    });
};
router.get('/addActivity', addActivity);
router.post('/addActivity', addActivity);

//------------------------------------------------------------------
/**
 * 删除活动场次信息(包括活动商品信息)
 * 参数 actId: 活动场次id
 */
function delActivity(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");   //设置跨域访问

    var actId = checkUtil.numberFromReq(req, 'actId')||0;
    if(actId<=0) {
        return luaUtil.ret_func_json(0,'参数错误',{},res);
    }

    luaUtil.delActivity(actId, function(err, ret){
        if(err) {
            return luaUtil.ret_func_json(0,CODE.OPERATION_FAILED,{},res);
        }
        return luaUtil.ret_func_json(1,CODE.OPERATION_SUCCESS,ret,res);
    });
};
router.get('/delActivity', delActivity);
router.post('/delActivity', delActivity);

//------------------------------------------------------------------
/**
 * 锁定活动商品项目
 * 参数 rId: 活动商品项目
 * 参数 uId: 用户id
 */
function lockActItem(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");   //设置跨域访问

    var rId = checkUtil.numberFromReq(req, 'rId')||0;
    var uId = checkUtil.numberFromReq(req, 'uId')||0;
    if(rId<=0 || uId<=0) {
        return luaUtil.ret_func_json(0,'参数错误',{},res);
    }

    luaUtil.lockActivityItem(rId, uId, function(err, ret){
        if(err) {
            return luaUtil.ret_func_json(0,CODE.OPERATION_FAILED,{},res);
        }
        return luaUtil.ret_func_json(1,CODE.OPERATION_SUCCESS,ret,res);
    });
};
router.get('/lockActItem', lockActItem);
router.post('/lockActItem', lockActItem);

//------------------------------------------------------------------
/**
 * 更新锁记录为成功
 * 参数 rId: 活动商品项目
 * 参数 uId: 用户id
 */
function updLockActItemSuccess(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");   //设置跨域访问

    var rId = checkUtil.numberFromReq(req, 'rId')||0;
    var uId = checkUtil.numberFromReq(req, 'uId')||0;
    if(rId<=0 || uId<=0) {
        return luaUtil.ret_func_json(0,'参数错误',{},res);
    }

    luaUtil.updLockActItemSuccess(rId, uId, function(err, ret){
        if(err) {
            return luaUtil.ret_func_json(0,CODE.OPERATION_FAILED,{},res);
        }
        return luaUtil.ret_func_json(1,CODE.OPERATION_SUCCESS,ret,res);
    });
};
router.get('/updLockActItemSuccess', updLockActItemSuccess);
router.post('/updLockActItemSuccess', updLockActItemSuccess);

//------------------------------------------------------------------
/**
 * 解锁活动商品项目
 * 参数 rId: 活动商品项目
 * 参数 uId: 用户id
 */
function unlockActItem(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");   //设置跨域访问

    var rId = checkUtil.numberFromReq(req, 'rId')||0;
    var uId = checkUtil.numberFromReq(req, 'uId')||0;
    if(rId<=0 || uId<=0) {
        return luaUtil.ret_func_json(0,'参数错误',{},res);
    }

    luaUtil.unlockActivityItem(rId, uId, function(err, ret){
        if(err) {
            return luaUtil.ret_func_json(0,CODE.OPERATION_FAILED,{},res);
        }
        return luaUtil.ret_func_json(1,CODE.OPERATION_SUCCESS,ret,res);
    });
};
router.get('/unlockActItem', unlockActItem);
router.post('/unlockActItem', unlockActItem);

//------------------------------------------------------------------
/**
 * 自动解锁超时商品项目
 * 参数 expLim: 超时时长,单位秒,默认300秒
 */
function unlockExpireItem(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");   //设置跨域访问

    var expLim = checkUtil.numberFromReq(req, 'expLim')||0;
    if(expLim<=0) {
        expLim=300;// 默认5分钟
    }

    luaUtil.unlockExpireItem(expLim, function(err, ret){
        if(err) {
            return luaUtil.ret_func_json(0,CODE.OPERATION_FAILED,{},res);
        }
        return luaUtil.ret_func_json(1,CODE.OPERATION_SUCCESS,ret,res);
    });
};
router.get('/unlockExpireItem', unlockExpireItem);
router.post('/unlockExpireItem', unlockExpireItem);


//------------------------------------------------------------------

module.exports = router;
